﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Tools;
using System.Text.RegularExpressions;

namespace Pixysoft.Weblications.Sango.Formular
{
    class PlayerFormular
    {
        private const int gender_male = 1;

        private const int gender_female = 0;

        public const int default_fame_serialcode = 0;

        public const int default_number_empty = -1;

        public static long default_refresh_date = DateTimeCoder.Instance.HourToMilliSecond(5);

        /// <summary>
        /// 军魂兑换银币
        /// </summary>
        public static double default_soul_to_cash = 1.27;

        /** 捐赠冷却时间 */
        public static long default_donate_cooltimespan = DateTimeCoder.Instance.HourToMilliSecond(1);

        /** 全局刷新时间 */
        public static int refresh_global = DateTimeCoder.Instance.HourToMilliSecond(5);

        /** 初始化玩家等级 */
        public static int default_startlevel_player = 1;

        /** 新建战略等级 */
        public static int default_startlevel_strategy = 0;

        // 默认阵法对应战略等级
        public static int default_tacticallevel_strategy = 1;

        /** 初始化现金 */
        public const int default_cashvalue = 1000;

        /** 初始化最大现金 */
        public const int default_maxcashvalue = 2000;

        /** 初始化粮草 */
        public const int default_resourcevalue = 1000;

        /** 初始化最大粮草 */
        public const int default_maxresourcevalue = 2000;

        /** 初始化兵力 */
        public const int default_soldiervalue = 1000;

        /** 初始化最大兵力 */
        public const int default_maxsoldiervalue = 1000;

        /** 初始化商魂 */
        public const int default_soulvalue = 50;

        /** 初始化金币 */
        public static int default_goldvalue = 200;

        /** 初始化仓库空间 */
        public static int default_storagecount = 5;


        // 默认巡视次数
        public const int default_scoutcount = 12;
        // 默认征收掉落金币
        public const int default_scout_dropgold = 10;
        // 玩家最大等级
        public const int default_player_maxlevel = 100;

        // 玩家相关

        /// <summary>
        /// 长度 = 10，只能有数字、字母、中文、下划线
        /// </summary>
        /// <param name="nickname"></param>
        /// <returns></returns>
        public static bool isValidNickname(string value)
        {
            if (value == null)
                return false;

            if (Regex.IsMatch(value, "～|·|！|＠|＃|￥|％|＾|＆|×|（|）|—|—|－|＋|＝|｛|【|｝|】|｜|＼|：|；|“|‘|《|，|》|。|？|、"))
                return false;

            String validValue = ContentReviewHelper.Replace(value, 8);

            if (!value.Equals(validValue, StringComparison.OrdinalIgnoreCase))
                return false;

            return SettingHelper.OnlyMatch(value.Trim(), "[\u0391-\uFFE5\u4e00-\u9fa5a-zA-Z0-9_]{1,8}");
        }

        /// <summary>
        /// 初始化玩家信息
        /// </summary>
        /// <param name="profile"></param>
        public static void InitializeProfile(Entities.Usr_Profile profile)
        {
            profile.Userlevel = default_startlevel_player;
            profile.Viplevel = SysVipLevelCollection.VIP_0;
            profile.Cashvalue = default_cashvalue;
            profile.Maxcashvalue = default_maxcashvalue;
            profile.Soulvalue = default_soulvalue;
            profile.Famevalue = 0;
            profile.Resourcevalue = default_resourcevalue;
            profile.Maxresourcevalue = default_maxresourcevalue;
            profile.Soldiervalue = default_soldiervalue;
            profile.Maxsoldiervalue = default_maxsoldiervalue;
            profile.Goldvalue = default_goldvalue;
            profile.Totalchargegoldvalue = 0;
            profile.Nationtypecode = NationTypecodeCollection.CAMP_NONE;
        }

        /// <summary>
        /// 初始化玩家信息 set before fame
        /// </summary>
        /// <param name="orm"></param>
        public static void InitializeProperty(Entities.Usr_Property orm)
        {
            orm.Trainingcount = SysQueueHelper.searchDefaultByType(CtiQueueTypecodeCollection.OFFICER_UPGRADE).Count;
            orm.Buildingcount = SysQueueHelper.searchDefaultByType(CtiQueueTypecodeCollection.BUILDING_UPGRADE).Count;

            orm.Bannercount = 0;
            orm.Repositorycount = default_storagecount;
            orm.Usedrepositorycount = 0;
            orm.Partycontributionvalue = 0;

            orm.Hiredofficercount = 0;
            orm.Officercount = SysFameSettingHelper.getFrame(orm.Fametitletypecode).Maxofficercount;

            orm.Mapserialcode = WarStageHelper.getStartMap().Mapserialcode;
        }

        /// <summary>
        /// 加班巡视次数
        /// </summary>
        /// <param name="nextCount"></param>
        /// <returns></returns>
        public static long getForceScoutGoldprice(long nextCount)
        {
            //2开始，每次+2

            return 2 * nextCount * 2;
        }

        /// <summary>
        /// 巡视兑换产出银币
        /// </summary>
        /// <param name="playerLevel"></param>
        /// <returns></returns>
        public static long getScoutValue(long playerLevel, long buildingLevel)
        {
            return (long)Math.Round((1500 / 12f + 273f * playerLevel / 24) * (1 + 0.01 * buildingLevel));
        }


        /// <summary>
        /// 获取性别
        /// </summary>
        /// <param name="avatarcode"></param>
        /// <returns></returns>
        public static long GetGender(long avatarcode)
        {
            Entities.Sys_Officer orm = SysOfficerHelper.searchByAvatarcode(avatarcode);
            return orm.Sex;
        }

        /// <summary>
        /// 是否刷新征收
        /// </summary>
        /// <param name="lastdate"></param>
        /// <returns></returns>
        public static bool isNextScoutDay(double lastdate)
        {
            double refreshdate = GlobalTimer.Instance.ToTimeSequence(DateTimeCoder.Instance.GetHeadOfToday()) + PlayerFormular.default_refresh_date;

            return SettingHelper.IsDailyRefresh(refreshdate, lastdate);
        }

        /// <summary>
        /// 是否刷新津贴
        /// </summary>
        /// <param name="lastdate"></param>
        /// <returns></returns>
        public static bool isNextAllowanceDay(double lastdate)
        {
            double refreshdate = GlobalTimer.Instance.ToTimeSequence(DateTimeCoder.Instance.GetHeadOfToday()) + PlayerFormular.default_refresh_date;

            return SettingHelper.IsDailyRefresh(refreshdate, lastdate);
        }

        /// <summary>
        /// 巡视冷却时间
        /// </summary>
        /// <param name="history"></param>
        /// <returns></returns>
        public static long createScoutCooldownTimespan(Pixysoft.Weblications.Sango.Entities.Usr_Activity history)
        {
            return DateTimeCoder.Instance.MinuteToMilliSecond(3);
        }

        /// <summary>
        /// 掉落金币概率 10%
        /// </summary>
        /// <param name="playerLevel"></param>
        /// <returns></returns>
        public static bool dropScoutGoldvalue(long playerLevel)
        {
            return SettingHelper.RandomNext(100) < 10;
        }

        /// <summary>
        /// 捐赠换取声望，所需的捐赠值
        /// </summary>
        /// <param name="playerLevel"></param>
        /// <returns></returns>
        public static long getDonateRequiredSoulvalue(long playerLevel)
        {
            //得到增量 =20, 初始化100军魂。

            return 100 + 50 * playerLevel;
        }

        /// <summary>
        /// 捐赠换取声望值
        /// </summary>
        /// <param name="playerLevel"></param>
        /// <returns></returns>
        public static long getDonateFamevalue(long playerLevel)
        {
            double soulvalue = getDonateRequiredSoulvalue(playerLevel);

            double cashvalue = soulvalue / default_soul_to_cash;

            double famevalue = cashvalue / 1000;

            if (famevalue < 1)
                famevalue = 1;

            return (long)Math.Round(famevalue);
        }

        /// <summary>
        /// 能否购买行动力
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="activity"></param>
        /// <returns></returns>
        public static bool canBuyActionPoint(Pixysoft.Weblications.Sango.Entities.Usr_Profile profile, Pixysoft.Weblications.Sango.Entities.Usr_Activity activity)
        {
            //查看VIP等级

            Entities.Sys_Vipsetting setting = SysVipSettingHelper.search(profile.Viplevel);

            if (setting.Extrabattleqty < 0)
                return true;

            if (activity.Battlebuycount >= setting.Extrabattleqty)
                return false;

            return true;
        }

        /// <summary>
        /// 购买行动力的价格
        /// </summary>
        /// <param name="activity"></param>
        /// <returns></returns>
        public static long getActionPointGoldPrice(Pixysoft.Weblications.Sango.Entities.Usr_Activity activity)
        {
            return 50 * activity.Battlebuycount * 50;
        }
    }
}
